var errNodes = [];
function InitGoJs() {
    if (window.goSamples) goSamples();  // init for these samples -- you don't need to call this
    var objGO = go.GraphObject.make;

    // initialize main Diagram
    myDiagram = objGO(go.Diagram, "myDiagramDivErrMsg", {
        maxSelectionCount: 0,
        padding: new go.Margin(0, 5, 0, 5),
        "undoManager.isEnabled": true,
        allowHorizontalScroll: false,
        allowVerticalScroll: true,
        allowZoom: false,
        allowSelect: false,
        //autoScale: go.Diaguram.Uniform,
        initialContentAlignment: go.Spot.TopLeft,
        //fixedBounds: new go.Rect(0, 0, 550, 600),
        //  alignment: go.GridLayout.Position,, spacing: new go.Size(1, 1)
        layout: objGO(go.GridLayout,
            {
                wrappingColumn: 1, cellSize: new go.Size(1, 1)
            })
    });

    myDiagram.nodeTemplateMap.add("",
        objGO(go.Node, "Auto", { movable: false, deletable: false, selectable: false, padding: new go.Margin(0, 10, 5, 0) },
            objGO(go.Shape, "RoundedRectangle",
                {
                    desiredSize: new go.Size(200, 35),
                    fill: "gray",
                    stroke: "gray",
                    strokeWidth: 0
                },
                new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify),
                new go.Binding("stroke", "color"),
                new go.Binding("fill")),
            objGO(go.TextBlock,
                {
                    margin: new go.Margin(7, 7, 7, 7), font: "16px sans-serif",
                    minSize: new go.Size(16, 16), stroke: "#F5F5F5",
                    textAlign: "center"
                },
                new go.Binding("font").makeTwoWay(),
                new go.Binding("text").makeTwoWay())
        ));

    myDiagram.groupTemplateMap.add("OfNodes",
        objGO(go.Group, go.Panel.Auto,
            {
                movable: false,//禁止拖动
                deletable: false,//禁止删除
                selectable: false,
                background: "#cccccc",
                padding: new go.Margin(5, 0, 5, 0),
                layout: objGO(go.GridLayout,
                    {
                        alignment: go.GridLayout.LeftToRight,
                        cellSize: new go.Size(1, 1)
                    })
                //, spacing: new go.Size(10, 3)
            },
            new go.Binding("position", "pos", go.Point.parse).makeTwoWay(go.Point.stringify),
            new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify),

            objGO(go.Shape, "RoundedRectangle",
                { fill: "#cccccc", stroke: "black", strokeWidth: 0 },
                new go.Binding("stroke", "color")),
            objGO(go.Panel, "Vertical",  // title above Placeholder
                objGO(go.Panel, "Horizontal",
                    { stretch: go.GraphObject.Vertical, background: '#cccccc' },
                    new go.Binding("background", "color"),
                    objGO(go.TextBlock, "RoundedRectangle",
                        {
                            margin: new go.Margin(0, 0, 0, 5),
                            alignment: go.Spot.Left,
                            font: "bold 24px sans-serif",
                            textAlign: "left",
                            stroke: "black"
                        },
                        new go.Binding("text", "text").makeTwoWay(),
                        new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify))
                ),  // end Horizontal Panel
                objGO(go.Placeholder,
                    { padding: 12, alignment: go.Spot.TopLeft })
            )  // end Vertical Panel
        ));  // end Group and call to add to template Map
        myDiagram.addDiagramListener("ObjectDoubleClicked", function (e) {
            let nodeData = e.subject.part.data;
            if (!nodeData.isGroup) {
                let url = "faultHis.html?key=" + nodeData.key;
                layer.open({
                    type: 2,
                    title: '故障历史信息',
                    shadeClose: true,
                    area: ['900px', '550px'],
                    content: [url, 'no'],
                });
            }
        })
}
//添加故障组
function AddGroups(groupsData) {
    var groupsArray = []
    var groupsJson = groupsData;
    for (var idx in groupsJson) {
        groupsArray.push({ "isGroup": true, "text": groupsJson[idx].GROUPNAME, "key": groupsJson[idx].GROUPID, "category": "OfNodes", "size": "1590 NAN" });//,"size":"520 380"  ,"size":"220 800"
    }
    myDiagram.model.addNodeDataCollection(groupsArray);
}
//添加故障点
function AddNodes(nodesData) {
    var nodesArray = [];
    var nodesJson = nodesData;
    for (var idx in nodesJson) {
        if (nodesJson[idx].SHOWNAME.length > 9) {
            nodesArray.push({ "group": nodesJson[idx].GROUPID, "text": nodesJson[idx].SHOWNAME, "key": parseInt(nodesJson[idx].CONTROLID), "category": "", "font": "14px sans-serif" });//,"size":"520 380"  ,"size":"220 800"
        }
        else {
            nodesArray.push({ "group": nodesJson[idx].GROUPID, "text": nodesJson[idx].SHOWNAME, "key": parseInt(nodesJson[idx].CONTROLID), "category": "" });//,"size":"520 380"  ,"size":"220 800"
        }
        //errNodes.add(nodesJson[idx].CONTROLID, "0");
    }
    myDiagram.model.addNodeDataCollection(nodesArray);
}
//更新数据
function UpdateData(nodeDatas) {
    var dataJson = nodeDatas;
    for (var idx in dataJson) {
        try {
            setColor(dataJson[idx].ControlID, dataJson[idx].Color);
            errNodes[dataJson[idx].ControlID] = dataJson[idx].Color;
        } catch (error) {
            console.log(error);
        }
    }
}
//修改颜色
function setColor(_key, clr) {
    var nodeData = myDiagram.model.findNodeDataForKey(parseInt(_key));
    if (nodeData != null) {
        myDiagram.model.setDataProperty(nodeData, 'fill', clr);
    }
}
//故障点闪烁线程
setInterval("ErrNodeTwinkle()", 300);
function ErrNodeTwinkle() {
    //故障点闪烁
    for (var item in errNodes) {
        if (errNodes[item] == "red") {
            setColor(item, "gray");
        }
    }
}